<!--设备本地日志-->
<template>
  <div class="device-log">
    <div class="header">
      <div class="title">设备列表</div>
    </div>
    <el-form :inline="true" class="search-form">
      <el-form-item>
        <el-input v-model="deviceId" v-only-numbers placeholder="请输入设备ID" clearable @keyup.enter.native="queryList('search')">
          <el-button slot="append" class="search-icon" icon="icon-sousuo" @click="queryList('search')"></el-button>
        </el-input>
        <el-tooltip content="设备ID只能输入数字" placement="top">
          <i class="iconfont icon-wenhao" style="font-size: 22px;"></i>
        </el-tooltip>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      size="medium"
      :header-cell-style="{ background: '#F5F6F7' }"
    >
      <el-table-column label="设备名称/ID">
        <template slot-scope="scope">
          <div>{{ scope.row.deviceName || "-" }}</div>
          <div>{{ scope.row.deviceId || "-" }}</div>
        </template>
      </el-table-column>
      <el-table-column label="设备状态" width="160px">
        <template slot-scope="scope">
          {{ scope.row.state === 'online' ? '在线' : '离线' }}
        </template>
      </el-table-column>
      <el-table-column label="SN">
        <template slot-scope="scope">
          {{ scope.row.deviceUuid || "-" }}
        </template>
      </el-table-column>
      <el-table-column label="所属产品/所属ID">
        <template slot-scope="scope">
          <div>{{ scope.row.productName || "-" }}</div>
          <div>{{ scope.row.productId || "-" }}</div>
        </template>
      </el-table-column>
      <el-table-column label="设备类型" width="100px">
        <template slot-scope="scope">
          {{ scope.row.deviceType == 1 ? "子设备" : (scope.row.deviceType == 0 ? "网关" : "门锁") }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="110px">
        <template slot-scope="scope">
          <el-button type="text" size="operate" @click="handleDetails(scope.row)">日志详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>
<script>
import { deviceList } from '@/api/device/log'
export default {
  name: 'deviceLog',
  directives: {
    onlyNumbers: {
      bind(el) {
        const input = el.querySelector('input')
        input.addEventListener('input', function() {
          this.value = this.value.replace(/\D/g, '')
        })
      }
    }
  },
  data() {
    return {
      deviceId: '', // 设备UUID
      tableData: [], // 列表数据
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页条数
      total: 0, // 列表数据总条数
      loading: true
    }
  },
  created() {
    this.queryList()
  },
  methods: {
    /**
     * 查询设备列表
     * current 当前页码
     * size 每页条数
     */
    queryList(condition) {
      this.currentPage = condition === 'search' ? 1 : this.currentPage
      this.loading = true
      const params = {
        current: this.currentPage,
        size: this.pageSize,
        deviceId: this.deviceId
      }
      deviceList(params).then(res => {
        this.tableData = res.data.data.records
        this.total = res.data.data.total
      }).catch(err => {
        this.$message.error(err.message)
      }).finally(() => {
        this.loading = false
      })
    },
    // 下载
    handleDetails(row) {
      this.$router.push({
        path: "/product/device-log/deviceDetails",
        query: {
          deviceId: row.deviceId,
          deviceName: row.deviceName
        }
      })
    },
    // 每页条数改变时触发
    handleSizeChange(val) {
      this.pageSize = val
      this.queryList()
    },
    // 当前页变时触发
    handleCurrentChange(val) {
      this.currentPage = val
      this.queryList()
    }
  }
}
</script>
<style lang="scss" scoped>
.device-log {
  height: 100%;

  .header {
    display: flex;
    align-items: center;
    margin-bottom: 17px;

    .title {
      font-size: 18px;
      font-weight: 600;
      color: #252525;
    }
  }

  .search-form {
    margin-bottom: 8px;

    .date {
      margin-left: 54px;

      ::v-deep .el-form-item__content {
        display: flex;
      }

      .date-picker {
        margin-right: 10px;
      }
    }

    .search-btn {
      float: right;

      .button-container {
        margin-left: 32px;
      }
    }

    .search-icon {
      cursor: pointer;
    }
  }
}

::v-deep .el-range-separator {
  color: #606266;
}

::v-deep .el-form-item__content {
  display: flex;

  .el-tooltip {
    margin-left: 6px;
  }
}
</style>
